
package com.shop.cloud.mall.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.mall.admin.service.ActiveBackgroundService;
import com.shop.cloud.mall.common.entity.ActiveBackground;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 活动背景
 *
 * @author
 * @date 2023-03-22 17:01:21
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/activebackground")
@Tag(description = "activebackground", name = "活动背景管理")
public class ActiveBackgroundController {

    private final ActiveBackgroundService activeBackgroundService;

    /**
     * 活动背景分页列表
     * @param page 分页对象
     * @param activeBackground 活动背景
     * @return
     */
    @Operation(summary = "活动背景分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ato.hasAuthority('mall:activebackground:index')")
    public R getPage(Page page, ActiveBackground activeBackground) {
        return R.ok(activeBackgroundService.page(page, Wrappers.query(activeBackground)));
    }

    /**
     * 活动背景查询
     * @param id
     * @return R
     */
    @Operation(summary = "活动背景查询")
    @GetMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:activebackground:get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(activeBackgroundService.getById2(id));
    }

    /**
     * 活动背景新增
     * @param activeBackground 活动背景
     * @return R
     */
    @Operation(summary = "活动背景新增")
    @SysLog("新增活动背景")
    @PostMapping
    @PreAuthorize("@ato.hasAuthority('mall:activebackground:add')")
    public R save(@RequestBody ActiveBackground activeBackground) {
		//校验是否有时间段重合的活动
		List<ActiveBackground> listActiveBackground = activeBackgroundService.list(Wrappers.<ActiveBackground>query().lambda()
				.nested(i -> i.ge(ActiveBackground::getValidBeginTime,activeBackground.getValidBeginTime()).le(ActiveBackground::getValidBeginTime,activeBackground.getValidEndTime()))
				.or(i -> i.le(ActiveBackground::getValidBeginTime,activeBackground.getValidBeginTime()).ge(ActiveBackground::getValidEndTime,activeBackground.getValidEndTime()))
				.or(i -> i.ge(ActiveBackground::getValidEndTime,activeBackground.getValidBeginTime()).le(ActiveBackground::getValidEndTime,activeBackground.getValidEndTime()))
		);
		if(listActiveBackground != null && listActiveBackground.size() > 0){
			return R.failed("该时间段有其他活动背景");
		}
        return R.ok(activeBackgroundService.save(activeBackground));
    }

    /**
     * 活动背景修改
     * @param activeBackground 活动背景
     * @return R
     */
    @Operation(summary = "活动背景修改")
    @SysLog("修改活动背景")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('mall:activebackground:edit')")
    public R updateById(@RequestBody ActiveBackground activeBackground) {
        return R.ok(activeBackgroundService.updateById1(activeBackground));
    }

    /**
     * 活动背景删除
     * @param id
     * @return R
     */
    @Operation(summary = "活动背景删除")
    @SysLog("删除活动背景")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:activebackground:del')")
    public R removeById(@PathVariable String id) {
        return R.ok(activeBackgroundService.removeById(id));
    }

}
